//
//  ViewController.swift
//  MineralRoot
//
//  Created by renyi on 16/4/25.
//  Copyright © 2016年 renyi. All rights reserved.
//

import UIKit
import Foundation

extension NSData {
    
    func MD5() -> NSData {
        let result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))!
        CC_MD5(bytes, CC_LONG(length), UnsafeMutablePointer<UInt8>(result.mutableBytes))
        return NSData(data: result)
    }
}


class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    @IBOutlet weak var getSn: UITextField!
    
    @IBOutlet weak var getMac: UITextField!

    @IBOutlet weak var password: UITextField!
    
    @IBAction func calcuateRoot(sender: AnyObject) {
        
        let sn = getSn.text;
        let mac = getMac.text;
        
        let key = "i8e%Fvj24nz024@d!c";
        
        let input = sn! + mac! + key;
        let data = input.dataUsingEncoding(NSUTF8StringEncoding);
        
        let plainString = data!.MD5;
        
        
        
        //let plainData = plainString.dataUsingEncoding(NSUTF8StringEncoding);
        let base64String = plainString().base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
        
       
        var rootpass = "";
        for index in base64String.characters.indices{
            var tmpChar = base64String[index];
            if (tmpChar == "+"){
                tmpChar = "-";
            }
            if (tmpChar == "/"){
                tmpChar = "_";
            }
            
            rootpass.append(tmpChar);
        }
        password.text = rootpass.substringToIndex(rootpass.startIndex.advancedBy(8));

    }

}

